package com.ricchieri.manage.bean;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedProperty;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.model.EmailTemplate;
import com.ricchieri.model.EmailToSendType;
import com.ricchieri.service.interf.IEmailTemplateService;
import com.ricchieri.service.interf.IEmailToSendTypeService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

public abstract class CommonEmailTemplateManageBean extends GenericManageBean{
	
	private static final long serialVersionUID = -3262836975289216792L;

	@ManagedProperty(value = "#{EmailToSendTypeService}")
	IEmailToSendTypeService emailTypeService;
	
	@ManagedProperty(value = "#{EmailTemplateService}")
	IEmailTemplateService emailTemplateService;
	
	private String template;
	protected String emailTemplateType;
	
	private EmailTemplate emailTemplate;
	
	private Map<String,String> templateType = null;
	
	@PostConstruct
	public void init() {
		emailTemplate = (EmailTemplate) FacesUtil.getParametersFlash(BusinessSettings.KEY_EMAIL_TEMPLATE);
		
		if(isRequeriedEmailTemplate() && emailTemplate == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_EMAIL_TEMPLATE);
		}
	}
	
	public Map<String, String> getTemplateType() {
		if (templateType == null){
			templateType = new TreeMap<String, String>();
			List<EmailToSendType> template = emailTypeService.getAllEmailToSendType();
			for (EmailToSendType tem : template) {
				templateType.put(FacesUtil.getMessage(tem.getDescription()), String.valueOf(tem.getId()));
			}
		}
		return templateType;
	}
	
	protected EmailToSendType obtainEmailType(String id) {
		List<EmailToSendType> template = emailTypeService.getAllEmailToSendType();
		for (EmailToSendType tem : template) {
			if(id.equals(String.valueOf(tem.getId()))) {
				return tem;
			}
		}
		return null;
	}
	
	public void setTemplateType(Map<String, String> templateType) {
		this.templateType = templateType;
	}
	
	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public abstract String getEmailTemplateType();

	public void setEmailTemplateType(String emailTemplateType) {
		this.emailTemplateType = emailTemplateType;
	}

	public IEmailToSendTypeService getEmailTypeService() {
		return emailTypeService;
	}

	public void setEmailTypeService(IEmailToSendTypeService emailTypeService) {
		this.emailTypeService = emailTypeService;
	}

	public IEmailTemplateService getEmailTemplateService() {
		return emailTemplateService;
	}

	public void setEmailTemplateService(IEmailTemplateService emailTemplateService) {
		this.emailTemplateService = emailTemplateService;
	}

	public EmailTemplate getEmailTemplate() {
		if(emailTemplate == null) {
			emailTemplate = new EmailTemplate();
		}
		return emailTemplate;
	}

	public void setEmailTemplate(EmailTemplate emailTemplate) {
		this.emailTemplate = emailTemplate;
	}
	
	public Map<String, String> getTemplateOptions() {
		Map<String, String> templateOptions = new TreeMap<String, String>();
		String description = null;
		
		if(StringUtils.isNotEmpty(getEmailTemplateType())) {
			description = String.valueOf(obtainEmailType(getEmailTemplateType()).getId());
		} else {
			description = ((TreeMap<String, String>) getTemplateType()).firstEntry().getValue();
		}
		
		if(description.equals(BusinessSettings.WELCOME)) {
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_USERNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_USERNAME));
			
		} else if (description.equals(BusinessSettings.RESERVATION) || description.equals(BusinessSettings.RESERVATION_UPDATED)) {
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_NUMBER), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_NUMBER));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_PRICE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_PRICE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_UP_ON), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_UP_ON));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DOWN_ON), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DOWN_ON));
			
		} else if(description.equals(BusinessSettings.CANCELATION)) {
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_NUMBER), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_RESERVATION_NUMBER));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR));
			
		} else if(description.equals(BusinessSettings.WARNING)) {
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_NOTICE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_NOTICE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY));
			
		} else if (description.equals(BusinessSettings.FIXED_RESERVATION)) {
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_ORIGIN));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DESTINY));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_PRICE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TRAVEL_PRICE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_UP_ON), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_UP_ON));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DOWN_ON), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DOWN_ON));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR));
			
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FINISH_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FINISH_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_START_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_START_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TIME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_TIME));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DAYS_WEEK), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DAYS_WEEK));
		} else if (description.equals(BusinessSettings.MULTIPLE_CANCELATION)) {
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FULLNAME));	
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_OPERATOR));
			
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FINISH_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_FINISH_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_START_DATE), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_START_DATE));
			templateOptions.put(FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DAYS_WEEK), FacesUtil.getMessage(BusinessSettings.EMAIL_TAG_DAYS_WEEK));
		}
		
		return templateOptions;
	}
	
	public abstract String save();
	
	public abstract Boolean isRequeriedEmailTemplate();
}
